package com.example.summary.controller;

import okhttp3.*;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class TicketHttpUtil {

    private static final long DEFAULT_CONNECT_TIMEOUT_MILLISECONDS = 100_000;
    private static final long DEFAULT_READ_TIMEOUT_MILLISECONDS = 100_000;
    private static final OkHttpClient builderClient = new OkHttpClient();

    public static String postXml(String url, String xml) {
        return OkHttpUtil.sendXmlPost("", url, xml, DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, DEFAULT_READ_TIMEOUT_MILLISECONDS, "UTF-8", "UTF-8", false);
    }

    public static String postJson(String url, String json) {
        return OkHttpUtil.sendJsonPost("", url, json, DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, DEFAULT_READ_TIMEOUT_MILLISECONDS, "UTF-8", "UTF-8", false);
    }

    public static String postKV(String url, Map<String, String> kv) {
        return OkHttpUtil.sendKVJsonPost("", url, kv, DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, DEFAULT_READ_TIMEOUT_MILLISECONDS, "UTF-8", "UTF-8", false);
    }

    public static String postKV(String url, Map<String, String> kv, String reqCharset, String resCharset) {
        return OkHttpUtil.sendKVJsonPost("", url, kv, DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, DEFAULT_READ_TIMEOUT_MILLISECONDS, reqCharset, resCharset, false);
    }

    public static String postParam(String url, String param) {
        return OkHttpUtil.sendTextPost("", url, param, DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, DEFAULT_READ_TIMEOUT_MILLISECONDS, "UTF-8", "UTF-8", false);
    }

    public static String postParam(String url) {
        return OkHttpUtil.sendTextPost("", url, "", DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, DEFAULT_READ_TIMEOUT_MILLISECONDS, "UTF-8", "UTF-8", false);
    }

    public static String getParam(String url, String param) {
        String newUrl = String.format("%s?%s", url, param);
        return OkHttpUtil.sendTextGet("", newUrl, DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, DEFAULT_READ_TIMEOUT_MILLISECONDS, "UTF-8", "UTF-8", false);
    }

    public static String postKVText(String url, Map<String, String> kv) {
        return OkHttpUtil.sendKVTextPost("", url, kv, DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, DEFAULT_READ_TIMEOUT_MILLISECONDS, "UTF-8", "UTF-8", false);
    }

    public static String postKVWithHeader(String traceId, String url, String json, Map<String, String> headers) throws IOException {
        OkHttpClient e = builderClient.newBuilder().connectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS).readTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS).build();
        RequestBody body1 = FormBody.create(MediaType.parse("application/json"), json);
        Request.Builder re = (new Request.Builder()).url(url);
        Iterator header = headers.entrySet().iterator();
        while (header.hasNext()) {
            Map.Entry h = (Map.Entry) header.next();
            re.addHeader((String) h.getKey(), (String) h.getValue());
        }
        Request request1 = re.post(body1).build();
        Response response = e.newCall(request1).execute();
        return new String(response.body().bytes(), "UTF-8");
    }

}
